<?php

namespace Home\Controller;
use Think\Controller;
use \Imp\api_demo\SmsDemo;
use \Model\SubscribeModel;
use \Tools\weixin_tools;
use \Imp\weixin\weixin;

//预约控制器
class SubscribeController extends Controller {
    function subscribe() {
    	$weixin = new weixin;  
		if (!isset($_GET["code"])){
			$redirect_url = 'http://olaqi.com/wysx/website/index.php/Subscribe/subscribe';
		    $jumpurl = $weixin->oauth2_authorize($redirect_url, "snsapi_userinfo", "123");
		    // dump($jumpurl);
		    Header("Location: $jumpurl");
			// echo "12";
		}else{
			//设置session
			if(!isset($_SESSION['weixin_openid'])) {
				// echo "y";exit;
				$access_token_oauth2 = $weixin->oauth2_access_token($_GET["code"]);
				// dump($access_token_oauth2);
				$userinfo = $weixin->oauth2_get_user_info($access_token_oauth2['access_token'], $access_token_oauth2['openid']);
				// dump($userinfo);
				$_SESSION['weixin_openid'] = $access_token_oauth2['openid'];
				$userinfo["visit_time"] = date('Y-m-d H:i:s', time());
				D('subscribe_visit')->add($userinfo);
			}
		}

		if($_POST) {
		    //验证用户提交的信息
		    $subscribe = new SubscribeModel();
		    //读取当前系统时间
		    $_POST['time'] = date('Y-m-d H:i:s', time());
		    $_POST['weixin_openid'] = $_SESSION['weixin_openid'];
		    // dump($_POST);
		    //表单验证
		    $data = $subscribe->create();
		    if (!$data){
				// 如果创建失败 表示验证没有通过 输出错误提示信息
				exit($subscribe->getError());
		    }

		    //检验验证码是否正确
		    $user_account = $_POST['user_account'];
		    $authCode = $_POST['authCode'];
		    $authCodeIsOk = $subscribe->checkAuthCode($user_account, $authCode);
		    if(!$authCodeIsOk){
				echo "验证码有误\n";
				exit ;
		    }
		    //将预约的用户加入到数据库中
		    $subscribe->add();
		    // dump("预约成功");
		    echo "true";
		    // $this ->redirect('Subscribe/success_subscribe');
		}
		else{
		    $school = new SubscribeModel();
		    $schoolList = $school->getSubscribeSchoolList();
		    // dump($schoolList);
		    $this -> assign('schoolList',$schoolList);
		    // exit;
		    $this -> display();
		}
	    }
	    
	        //获取验证码
	    public function getAuthCode() {
		// dump($_POST);
		$user_account = $_POST['user_account'];
		if(preg_match('/1\d{10}/',$user_account)) {
		    // echo "可以注册！";
		}else{
		    echo "请填写正确的手机号";
		    exit;
		}
		$getType = $_POST['getType'];
		if ($user_account) {
		    //验证手机号
		    $data = true;
		    //如果类型为注册类型，首先检测用户是否存在
		    if ($getType == 'subscribe') {
			$sqlCheck = array(
			    'user_account' => $user_account,
			);
			$db = D('subscribe');
			$info = $db->where($sqlCheck)->find();
			if ($info) {
			    echo "该用户已经预约";
			    exit;
			}
		    }

		    if ($data) {//如果验证成功
			//返回前端已发送
			echo "true";
			//实例化短信发送类
			$send = new SmsDemo;
			// echo "fa";
			// exit;
			//产生6位随机数
			$authCode = rand(100000, 999999);

			//向用户发送验证码
			$send->sendSms($user_account, $authCode); //若不想发送短信，请注释本行代码
			//将用户的手机号保存在"临时用户"数据库中
			//判断临时数据库中是否有用户，如果已经存在，则修改用户的验证码，否则添加用户验证码
			$sql = array(
			    'temp_user_tel' => $user_account,
			    // 'temp_user_authcode' => $authCode,
			);
			$db = D('temp_user');
			$info = $db->where($sql)->find();
			// dump("info:".$info);

			if ($info) { //如果临时表里有该用户，则更新新的验证码
			    $db->temp_user_authcode = $authCode;
			    $db->temp_user_authcode_time = date('y-m-d H:i:s', time());
			    $db->save();
			    // echo "更新成功";
			    exit;
			} else { //否则添加新的验证码
			    $sql2 = array(
				'temp_user_tel' => $user_account,
				'temp_user_authcode_time' => date('y-m-d H:i:s', time()),
				'temp_user_authcode' => $authCode,
			    );
			    $info = $db->add($sql2);
			    // echo "添加成功";
			}
		    } else { //如果验证失败
			// $this->assign('errorInfo', $um->getError());
			// $this ->display();
		    }
		}
	    }
	    
	    function success() {
		$this->display();
	    }
    
}